package com.starter.home.controller;

import com.starter.home.domain.entity.User;
import com.starter.home.service.IIndexService;
import com.starter.home.util.MyUtil;
import com.starter.home.util.ResponseResult;
import com.starter.home.util.constant.MessageConstant;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Optional;

/**
 */
@RestController
@RequestMapping(value = "/index")
public class IndexController {

    @Autowired
    private IIndexService indexService;

    @GetMapping(value="/getAdminIndex")
    @ResponseBody
    public ResponseResult getAdminIndex(HttpSession session) {
        Optional<User> user = MyUtil.getUser(session);
        if (user.isPresent()){
            return ResponseResult.success(indexService.getIndexForAdminData(user.get().getId()));
        }else {
            return ResponseResult.error(MessageConstant.SESSION_OUTTIME);
        }
    }

    @GetMapping(value="/getUserIndex")
    @ResponseBody
    public ResponseResult getUserIndex(HttpSession session) {
        Optional<User> user = MyUtil.getUser(session);
        if (user.isPresent()){
            return ResponseResult.success(indexService.getUserIndex(user.get().getId()));
        }else {
            return ResponseResult.error(MessageConstant.SESSION_OUTTIME);
        }
    }
}
